package com.dhcc.bpm.modules.orga.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.orga.entity.App;
import com.dhcc.bpm.modules.orga.entity.OnlineApply;
import com.dhcc.bpm.modules.orga.service.IAppApiService;
import com.dhcc.bpm.modules.orga.service.IAppService;
import com.dhcc.bpm.modules.orga.service.IOnlineApplyService;
import com.dhcc.bpm.modules.orga.vo.AppApiVO;
import com.dhcc.bpm.modules.orga.vo.OnlineApplyDetailVO;
import com.dhcc.bpm.modules.orga.vo.OnlineApplyVO;
import com.dhcc.bpm.modules.workflow.service.IWorkFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName OnlineApplyController
 * @Description 上线申请审批表 控制器
 * @Author daina
 * @Date 2020/2/24 19:25
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-orga/onlineapply")
@Api(value = "上线申请审批表", tags = "上线申请审批表接口")
public class OnlineApplyController extends BladeController {

	private IOnlineApplyService onlineApplyService;

	private IAppService appService;

	private IAppApiService appApiService;
	@Autowired
	private IWorkFlowService iWorkFlowService;
	/**
	 * @Description  详情
	 * @Author daina
	 * @Date 10:41 2020/2/25
	 * @param onlineApply
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.orga.vo.OnlineApplyDetailVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "上线申请审批表", notes = "传入onlineApply")
	@ApiLog("上线申请审批信息 详情")
	@Log(title = "上线申请审批信息 详情", businessType = BusinessType.SELECT)
	public R<OnlineApplyDetailVO> detail(OnlineApplyVO onlineApply) {
		App appDetail = null;
	//	OnlineApply onlineApplyetail = onlineApplyService.getOne(Condition.getQueryWrapper(onlineApply));
		OnlineApplyVO onlineApplyetail = onlineApplyService.selectOnlineAppById(onlineApply.getId());
		if(onlineApplyetail!=null){
			App app = new App();
			app.setId(onlineApplyetail.getAppId());
			appDetail = appService.getOne(Condition.getQueryWrapper(app));
		}

		List<AppApiVO> appApiList = appApiService.findApiList(appDetail, "1");
		OnlineApplyDetailVO onlineApplyDetailVO = new OnlineApplyDetailVO();
		onlineApplyDetailVO.setOnlineApply(onlineApplyetail);
		onlineApplyDetailVO.setApp(appDetail);
		onlineApplyDetailVO.setApiList(appApiList);
		return R.data(onlineApplyDetailVO);
	}

	/**
	 * @Description 上线申请审批表
	 * @Author daina
	 * @Date 10:41 2020/2/25
	 * @param onlineApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.entity.OnlineApply>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "上线申请审批信息 分页", notes = "传入onlineApply")
	@ApiLog("上线申请审批信息 分页")
	@Log(title = "上线申请审批信息 分页", businessType = BusinessType.SELECT)
	public R<IPage<OnlineApply>> list(OnlineApply onlineApply, Query query) {
		IPage<OnlineApply> pages = onlineApplyService.page(Condition.getPage(query), Condition.getQueryWrapper(onlineApply));
		return R.data(pages);
	}

	/**
	 * @Description 上线申请审批表
	 * @Author daina
	 * @Date 10:41 2020/2/25
	 * @param onlineApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.OnlineApplyVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "上线申请审批信息 分页", notes = "传入onlineApply")
	@ApiLog("上线申请审批信息 分页")
	@Log(title = "上线申请审批信息 分页", businessType = BusinessType.SELECT)
	public R<IPage<OnlineApplyVO>> page(OnlineApplyVO onlineApply, Query query) {
		IPage<OnlineApplyVO> pages = onlineApplyService.selectOnlineApplyPage(Condition.getPage(query), onlineApply);
		return R.data(pages);
	}

	/**
	 * @Description 新增 上线申请审批表
	 * @Author daina
	 * @Date 10:41 2020/2/25
	 * @param onlineApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入onlineApply")
	@ApiLog("新增上线申请审批信息")
	@Log(title = "新增上线申请审批信息", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody OnlineApply onlineApply) {
		return R.status(onlineApplyService.save(onlineApply));
	}

	/**
	 * @Description 修改 上线申请审批表
	 * @Author daina
	 * @Date 10:42 2020/2/25
	 * @param onlineApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改上线申请审批信息", notes = "传入onlineApply")
	@ApiLog("修改上线申请审批信息")
	@Log(title = "修改上线申请审批信息", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody OnlineApply onlineApply) {
		return R.status(onlineApplyService.updateById(onlineApply));
	}

	/**
	 * @Description 新增或修改 上线申请审批表
	 * @Author daina
	 * @Date 10:42 2020/2/25
	 * @param onlineApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改 上线申请审批信息", notes = "传入onlineApply")
	@ApiLog("新增或修改 上线申请审批信息")
	@Log(title = "新增或修改 上线申请审批信息", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody OnlineApply onlineApply) {
		return R.status(onlineApplyService.saveOrUpdate(onlineApply));
	}

	
	/**
	 * @Description 删除 上线申请审批表
	 * @Author daina
	 * @Date 10:42 2020/2/25
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除上线申请审批信息", notes = "传入ids")
	@ApiLog("删除上线申请审批信息")
	@Log(title = "删除上线申请审批信息", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		String[] arrayIds=ids.split(",");
		for (String id:  arrayIds) {
			iWorkFlowService.deleteRelated("n",id);
		}
		return R.status(onlineApplyService.deleteLogic(Func.toLongList(ids)));
	}

	
}
